package com.mall507.fivepeaks.activity.community;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.xdialog.XRadioGroupDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Route(path = "/community/CommunityAssociationEnterUI")
public class CommunityAssociationEnterUI extends BaseUI {
    Button submit;
    EditText name;
    EditText phone;
    EditText assication;
    EditText address;
    TextView area;
    
    private List<HashMap<String, Object>> areaList = new ArrayList<>();
    
    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_association_enter);
        getTitleView().setContent("填写入驻信息");
        submit = findViewById(R.id.submit);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        assication = findViewById(R.id.assication);
        address = findViewById(R.id.address);
        area = findViewById(R.id.area);
        address.setHorizontallyScrolling(true);
        choose();
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(name.getText()) || TextUtils.isEmpty(phone.getText()) || TextUtils.isEmpty(assication.getText()) || TextUtils.isEmpty(address.getText())){
                    alert("请完善信息再尝试提交");
                }else {
                    if (phone.getText().length()<11){
                        alert("请输入正确的联系电话");
                    }else {
                        if (assication.getText().toString().length() < 4){
                            alert("请输入协会简称(4至6字之间)");
                        }else {
                            enter();
                        }
                    }
                }
            }
        });
    }

    private void enter(){
        JSONObject params = new JSONObject();
        try {
            params.put("name", name.getText().toString());
            params.put("phoneNumber", phone.getText().toString());
            params.put("AssociationName", assication.getText().toString());
            params.put("areaId", area.getTag(R.id.main_release_area_value).toString());
            params.put("address",address.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        BaseHttp.getInstance().query("Association/joinAssociation", params, new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (200 == response.optInt("code")){
                    onBackPressed();
                }
                alert(response.optString("msg"));
                super.onSuccess(response);
            }
        });
    }

    private void choose(){
        BaseHttp.getInstance().query("Release/getList", new HttpCallBack() {
            @Override
            public void onSuccess(JSONObject response) {
                if (response.optInt("code") == 200) {
                    JSONObject data = response.optJSONObject("data");
                    JSONArray area = data.optJSONArray("area");
                    chooseArea(area);
                }
                super.onSuccess(response);
            }
        });
    }

    private void chooseArea(JSONArray params){
        if (params != null && params.length() > 0) {
            for (int i = 0; i < params.length(); i++) {
                JSONObject object = params.optJSONObject(i);
                HashMap<String, Object> map = new HashMap<>();
                map.put("name", object.optString("name"));
                map.put("value", object.optString("id"));
                areaList.add(map);
                if (i == 0) {//设置默认值,默认第一个为需要请求的值
                    area.setText(map.get("name").toString());
                    area.setTag(R.id.main_release_area_value, object.optString("id"));
                }

                area.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new XRadioGroupDialog().show(CommunityAssociationEnterUI.this, areaList, new XRadioGroupDialog.IRadioGroupDialogCallBack() {

                            @Override
                            public void confirm(HashMap<String, Object> selectedItem) {
                                area.setText(selectedItem.get("name").toString());
                                area.setTag(R.id.main_release_area_value, selectedItem.get("value").toString());
                            }

                            @Override
                            public void cancel() {

                            }
                        });
                    }
                });
            }
        }
    }
    
}
